/**
 * 
 */
package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import officeserver.log_error.UserException;
import officeserver.office.request.RequestException;

import GUI.main.MainClient;

/**
 * @author mramsey3
 * 
 */

public class LoginDialog extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    private final static int WIDTH = 400;
    private final static int HEIGHT = 210;

    private JPanel logoPanel;
    private JPanel centerPanel;
    private JPanel userPanel;
    private JLabel userLabel;
    private JTextField userNameField;
    private JPanel passwordPanel;
    private JLabel passwordLabel;
    private JPasswordField passwordField;
    private JPanel errorPanel;
    private JLabel errorMessage;
    private JPanel buttonPanel;
    private JButton loginButton;

    /**
     * 
     */
    public LoginDialog() {
	setSize(new Dimension(WIDTH, HEIGHT));
	setLocation(50, 50);
	setLayout(new BorderLayout());

	add(getLogoPanel(), BorderLayout.NORTH);

	add(getCenterPanel(), BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getLogoPanel() {
	if (logoPanel == null) {
	    logoPanel = new JPanel() {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(new Color(100, 150, 220));
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("the office 2.0", 12, getHeight() - 8);
		}
	    };
	    logoPanel.setPreferredSize(new Dimension(WIDTH, 75));
	}
	return logoPanel;
    }

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getUserPanel());
	    centerPanel.add(getPasswordPanel());
	    centerPanel.add(getErrorPanel());
	    centerPanel.add(getButtonPanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getUserPanel() {
	if (userPanel == null) {
	    userPanel = new JPanel();

	    userPanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.WEST;
	    gbcA.weightx = 1;
	    leftPanel.add(getUserLabel(), gbcA);
	    userPanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    rightPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.EAST;
	    gbcB.weightx = 1;
	    rightPanel.add(getUserNameField(), gbcB);
	    userPanel.add(rightPanel, BorderLayout.EAST);
	}
	return userPanel;
    }

    /**
     * @return
     */
    private JLabel getUserLabel() {
	if (userLabel == null) {
	    userLabel = new JLabel();

	    userLabel.setText("    username:");
	}
	return userLabel;
    }

    /**
     * @return
     */
    private JTextField getUserNameField() {
	if (userNameField == null) {
	    userNameField = new JTextField("enter username", 20);

	    userNameField.setEditable(true);
	}
	return userNameField;
    }

    /**
     * @return
     */
    private JPanel getPasswordPanel() {
	if (passwordPanel == null) {
	    passwordPanel = new JPanel();

	    passwordPanel.setLayout(new BorderLayout());

	    JPanel leftPanel = new JPanel();
	    leftPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcA = new GridBagConstraints();
	    gbcA.anchor = GridBagConstraints.WEST;
	    gbcA.weightx = 1;
	    leftPanel.add(getPasswordLabel(), gbcA);
	    passwordPanel.add(leftPanel, BorderLayout.WEST);

	    JPanel rightPanel = new JPanel();
	    rightPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbcB = new GridBagConstraints();
	    gbcB.anchor = GridBagConstraints.EAST;
	    gbcB.weightx = 1;
	    rightPanel.add(getPasswordField(), gbcB);
	    passwordPanel.add(rightPanel, BorderLayout.EAST);
	}
	return passwordPanel;
    }

    /**
     * @return
     */
    private JLabel getPasswordLabel() {
	if (passwordLabel == null) {
	    passwordLabel = new JLabel();

	    passwordLabel.setText("    password:");
	}
	return passwordLabel;
    }

    /**
     * @return
     */
    private JPasswordField getPasswordField() {
	if (passwordField == null) {
	    passwordField = new JPasswordField("enter password", 20);

	    passwordField.setEditable(true);
	}
	return passwordField;
    }

    /**
     * @return
     */
    private JPanel getErrorPanel() {
	if (errorPanel == null) {
	    errorPanel = new JPanel();

	    errorPanel.add(getErrorMessage());
	}
	return errorPanel;
    }

    /**
     * @return
     */
    private JLabel getErrorMessage() {
	if (errorMessage == null) {
	    errorMessage = new JLabel(" ");
	}
	return errorMessage;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();

	    buttonPanel.setPreferredSize(new Dimension(WIDTH, 35));

	    buttonPanel.add(getLoginButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private JButton getLoginButton() {
	if (loginButton == null) {
	    loginButton = new JButton("login");
	    loginButton.setMargin(new Insets(0, 0, 0, 0));
	    loginButton.setPreferredSize(new Dimension(75, 25));
	    loginButton.addActionListener(this);
	}
	return loginButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getLoginButton()) {
	    try {
		System.out.println(String.valueOf(getPasswordField()
			.getPassword()));
		MainClient.getConnection().userLogin(
			getUserNameField().getText(),
			String.valueOf(getPasswordField().getPassword()));
		try {
		    if (MainClient.getConnection().isLoggedIn()) {
			this.setVisible(false);
		    }
		} catch (Exception e1) {
		    e1.printStackTrace();
		}
	    } catch (UserException e1) {
		getErrorMessage().setText("Wrong User/Password Combination.");
		e1.printStackTrace();
	    } /*
	       * catch (IOException e1) { e1.printStackTrace();
	       * getErrorMessage().setText(
	       * "Network Error. Contact Administrator."); } catch
	       * (ClassNotFoundException e1) { e1.printStackTrace(); } catch
	       * (RequestException e1) { e1.printStackTrace();
	       * getErrorMessage().setText(
	       * "Network Error. Contact Administrator."); }
	       */
		catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (ClassNotFoundException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (RequestException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
    }
}
